package com.saa.common.utils;

import ins.framework.exception.BusinessException;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


public class HttpRequestUtil {

    private static ThreadLocal<String> stringThreadLocal = new ThreadLocal<>();

    public static String getMakecom() {
        return stringThreadLocal.get();
    }

    public static void setMakecom(String makecom) {
        stringThreadLocal.set(makecom);
    }

    private static HttpServletRequest initRequest(){
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attrs == null) {
            throw new BusinessException("当前线程中不存在 Request 上下文",false);
        }
        return attrs.getRequest();
	}
	
	public static String getMakeCom(){
        String makeCom = getMakecom();
        if (makeCom == null){
            makeCom = (String) initRequest().getAttribute("makeCom");
        }
		return makeCom;
	}
	public static String getUserCode(){
		return (String) initRequest().getAttribute("userCode");
	}
	public static String getUserName(){
		return (String) initRequest().getAttribute("userName");
	}
	public static HttpServletRequest getRequest(){
		return initRequest();
	}
}
